package com.beici.waterghost.controller;

import com.beici.waterghost.model.classBox.classboxNode;
import com.beici.waterghost.model.resultes;
import com.beici.waterghost.model.user;
import com.beici.waterghost.service.classBoxInterface.classBoxNodeService;
import com.beici.waterghost.utils.contextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

/**
 * @auther WaterGhost
 * @date 2022/3/22 20:01
 */
@RestController
@CrossOrigin
@RequestMapping("/cbnode")
public class classBoxNodeController {

    @Autowired
    private classBoxNodeService nodeService;

    @GetMapping("/node/{abid}")
    public resultes getClassBoxNodes(@PathVariable("abid") Long abId){
        return nodeService.getAlbumNodes(abId);
    }

    @GetMapping("/detail/{cbid}")
    public resultes getClassBoxDetailNodes(@PathVariable("cbid") Long cbId){
        return nodeService.getNodeDetails(cbId);
    }
    @PostMapping("/node")
    public resultes putNode(classboxNode node, HttpServletRequest request){
        if (node.getCbSercurityScore()<100){
            return resultes.error("请检查敏感词汇在进行提交");
        }
        if (node.getCbContext().length()<40){
            return resultes.error("内容长度不达标40,请勿水贴");
        }
        node.setCbIp(request.getRemoteAddr());
        // 添加
        user u = (user) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        node.setCbUserId(u.getUserId());
        return nodeService.addNode(node);
    }
    @PostMapping("/verifity")
    public resultes verifityNode(String cbName,String cbMess,String cbContext){
        // 总分100
        int score = 100;
        boolean b = contextUtils.veifityContext(cbName);
        boolean b1 = contextUtils.veifityContext(cbMess);
        boolean b2 = contextUtils.veifityContext(cbContext);
        StringBuffer stringBuffer = new StringBuffer();
        if (!b){
            score=score-20;
            stringBuffer.append("  标题存在问题,");
        }
        if (!b1){
            score=score-20;
            stringBuffer.append("  描述存在问题,");
        }
        if (!b2){
            score=score-20;
            stringBuffer.append("  内容存在问题,");
        }
        return resultes.success("检测完毕总分为："+score+stringBuffer.toString(),score);

    }

}
